package com.tjnu.losca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjnu.losca.mapper.UserinfoMapper;
import com.tjnu.losca.pojo.Userinfo;
import com.tjnu.losca.service.IUserinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author losca
 * @since 2022-02-13
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements IUserinfoService {



    @Override
    public IPage<Userinfo> list(int pageNum, int pageSize, String search) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<Userinfo>();
        if(StringUtils.isNotBlank(search)) {
            wrapper.like("name",search)
                    .or().like("email",search)
                    .or().like("tel",search);
        }
        return this.page(new Page<Userinfo>(pageNum,pageSize),wrapper);
    }

    @Override
    public boolean del(String userid) {
        //通过用户id获取用户
        Userinfo user = this.getById(userid);
        //0代表禁用 1代表可用
        user.setStatus(0);
        //修改用户状态
        return this.updateById(user);
    }

    @Override
    public boolean rec(String userid) {
        Userinfo user = this.getById(userid);
        //0代表禁用 1代表可用
        user.setStatus(1);
        return this.updateById(user);
    }

    @Override
    public boolean mulDel(String[] ids) {
        //通过ids获取多个用户
        List<Userinfo> users = new ArrayList<>();
        //遍历ids把用户状态设置为0
        for (String id : ids) {
            Userinfo userinfo = this.getById(id);
            userinfo.setStatus(0);
            users.add(userinfo);
        }
        //批量修改用户状态
        return this.updateBatchById(users);
    }

    @Override
    public boolean update(Userinfo userinfo) {
        return this.updateById(userinfo);
    }
}
